package spass.raytracer;

import org.springframework.stereotype.Component;

import spass.geometry.Point;
import spass.geometry.Ray;
import spass.geometry.Vector;
import spass.renderer.PixelRenderer;

@Component
public class EyeRayCreator {

    private static final int FOCAL_LENGTH = 1;
    private static final double CANVAS_SCALING_FACTOR = 1000;

    public Ray createRay(PixelRenderer renderer, int x, int y) {
        Vector direction = getDirection(renderer, x, y);
        Point origin = Point.origin();

        return new Ray(origin, direction);
    }

    private Vector getDirection(PixelRenderer renderer, int x, int y) {
        int width = renderer.getWidth();
        int height = renderer.getHeight();

        double directionX = scaleDown(x, width);
        double directionY = scaleDown(height - y, height);

        return new Vector(directionX, directionY, FOCAL_LENGTH);
    }

    private double scaleDown(int pos, int max) {
        double scaledMax = max / CANVAS_SCALING_FACTOR;
        double scaledPos = (double) pos / max;
        double position = scaledPos * scaledMax;
        double offset = scaledMax / 2;

        return position - offset;
    }

}
